//
//  BaseDataSource.m
//  Weikan
//
//  Created by maming on 11-8-10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BaseDataSource.h"
#import "DataSource.h"
#import "NSDate+Helper.h"

@implementation BaseDataSource

+ (BaseDataSource*)shareInstance
{
	static BaseDataSource * instance = nil;
	if (instance == nil)
	{
		instance = [[BaseDataSource alloc]init];
	}
	return instance;
}

- (UserInfo*)userinfoParser:(NSData*)data
{
    UserInfo* info = [[[UserInfo alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* userinfoDict = (NSDictionary*)data;
        info.member_id = [userinfoDict objectForKey:@"c_member_id"];
        info.name = [userinfoDict objectForKey:@"name"];
        info.image_filename = [userinfoDict objectForKey:@"image_filename"];
        info.sex = [userinfoDict objectForKey:@"sex"];
        info.email = [userinfoDict objectForKey:@"email"];
        info.creat_time = [userinfoDict objectForKey:@"create_time"];
    }
    NSLog(@"parser");
    return info;
}

- (TripPageInfo*)recommendinfoParser:(NSData*)data
{
    TripPageInfo* tripPageInfo = [[[TripPageInfo alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* infoDict = (NSDictionary*)data;
        
        id elementinfoArray = [infoDict objectForKey:@"data"];
        NSMutableArray* recommendinfoArray = [[NSMutableArray alloc] init];
        if ([elementinfoArray isKindOfClass:[NSArray class]]) {
            NSArray* array = (NSArray*)elementinfoArray;
            for (NSDictionary* dict in array) {
                RecommendTripInfo* info = [[RecommendTripInfo alloc] init];
                info.travellineid = [dict objectForKey:@"c_travel_line_id"];
                info.name = [dict objectForKey:@"name"];
                info.author = [dict objectForKey:@"author"];
                info.creattime = [dict objectForKey:@"create_time"];
                info.memberid = [dict objectForKey:@"c_member_id"];
                info.coverurl = [dict objectForKey:@"cover"];
                info.praisecount = [dict objectForKey:@"praise_count"];
                info.favorcount = [dict objectForKey:@"favor_count"];
                info.commentcount = [dict objectForKey:@"comment_count"];
                info.membericon = [dict objectForKey:@"c_member_icon"];
                [recommendinfoArray addObject:info];
                [info release];
            }
        }
        [tripPageInfo.tripArray addObjectsFromArray:recommendinfoArray];
        [recommendinfoArray removeAllObjects];
        [recommendinfoArray release];
        
        id tripPageDict = [infoDict objectForKey:@"navi"];
        if ([tripPageDict isKindOfClass:[NSDictionary class]]) {
            NSNumber* currentPageNumber = [tripPageDict objectForKey:@"current_page"];
            tripPageInfo.currentPageId = [currentPageNumber intValue];
            tripPageInfo.nextPage = (BOOL)[tripPageDict objectForKey:@"next"];
            NSNumber* totlePageCountNumber  = [tripPageDict objectForKey:@"total_page"];
            if ([totlePageCountNumber isKindOfClass:[NSNumber class]]) {
                tripPageInfo.totlePageCount = [totlePageCountNumber intValue];
            }
        }
    }
    return tripPageInfo;
}

- (NSString*)creattripinfoParser:(NSData*)data
{
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* array = (NSDictionary*)data;
        return [array objectForKey:@"c_travel_line_id"];
    }
    return 0;
}

- (NSMutableArray*)newCitylistinfoParser:(NSData *)data
{
    NSMutableArray* citylistinfoArray = [[[NSMutableArray alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* dict = (NSDictionary*)data;
        [[DataSource shareInstance].cityDict setValue:[dict objectForKey:@"hot_city"] forKey:@"hot"];
        NSDictionary* cityInfoDict = [dict objectForKey:@"alpha_list"];
        NSArray* keyArray = [cityInfoDict allKeys];
        for (NSString* key in keyArray) {
            [[DataSource shareInstance].cityDict setValue:[cityInfoDict objectForKey:key] forKey:key];
            NSArray* array = [cityInfoDict objectForKey:key];
            for (NSDictionary* dict in array) {
                CityInfo* info = [[CityInfo alloc] init];
                info.cityid = [dict objectForKey:@"c_city_id"];
                info.name = [dict objectForKey:@"name"];
                info.pinyin = [dict objectForKey:@"pinyin"];
                [citylistinfoArray addObject:info];
                [info release];
            }
        }
    }
    
    [[DataSource shareInstance] setCityList:citylistinfoArray];
    return citylistinfoArray;
}

- (NSMutableArray*)citylistinfoParser:(NSData*)data
{
    NSMutableArray* citylistinfoArray = [[[NSMutableArray alloc] init] autorelease];
    if ([data isKindOfClass:[NSArray class]]) {
        NSArray* array = (NSArray*)data;
        for (NSDictionary* dict in array) {
            CityInfo* info = [[CityInfo alloc] init];
            info.cityid = [dict objectForKey:@"c_city_id"];
            info.name = [dict objectForKey:@"name"];
            info.pinyin = [dict objectForKey:@"pinyin"];
            [citylistinfoArray addObject:info];
            [info release];
        }
    }
    [[DataSource shareInstance] setCityList:citylistinfoArray];
    NSMutableArray* nameArray = [[NSMutableArray alloc] init];
    for (CityInfo* info in citylistinfoArray) {
        [nameArray addObject:info.name];
    }
    NSString* str = [NSString stringWithFormat:@"%@/lvtu/%@",[[DataSource shareInstance] file_getAppCachePath],@"cityList.plist"]; 
    [nameArray writeToFile:str atomically:YES];
    [nameArray release];
    return citylistinfoArray;
}

- (ElementPageInfo*)elementinfoParser:(NSData*)data
{
    ElementPageInfo* elementPageInfo = [[[ElementPageInfo alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* elementinfoDict = (NSDictionary*)data;
        
        id elementinfoArray = [elementinfoDict objectForKey:@"data"];
        NSMutableArray* infoArray = [[NSMutableArray alloc] init];
        if ([elementinfoArray isKindOfClass:[NSArray class]]) {
            NSArray* array = (NSArray*)elementinfoArray;
            for (NSDictionary* dict in array) {
                PlaceInfo* info = [[PlaceInfo alloc] init];
                info.shopid = [dict objectForKey:@"c_shop_id"];
                info.name = [dict objectForKey:@"name"];
                info.review = [dict objectForKey:@"review"];
                info.imageurl = [dict objectForKey:@"image_url"];
                [infoArray addObject:info];
                [info release];
            }
        }
        [elementPageInfo.elementArray addObjectsFromArray:infoArray];
        [infoArray removeAllObjects];
        [infoArray release];
        
        id elementPageDict = [elementinfoDict objectForKey:@"navi"];
        if ([elementPageDict isKindOfClass:[NSDictionary class]]) {
            NSNumber* currentPageNumber = [elementPageDict objectForKey:@"current_page"];
            elementPageInfo.currentPageId = [currentPageNumber intValue];
            elementPageInfo.nextPage = (BOOL)[elementPageDict objectForKey:@"next"];
            NSNumber* totlePageCountNumber = [elementPageDict objectForKey:@"total_page"];
            if ([totlePageCountNumber isKindOfClass:[NSNumber class]]) {
                elementPageInfo.totlePageCount = [totlePageCountNumber intValue];
            }
        }
    }
    return elementPageInfo;
}

- (NSMutableArray*)traveldetailinfoParser:(NSData*)data
{
    NSMutableArray* traveldetailinfoArray = [[[NSMutableArray alloc] init] autorelease];
    if ([data isKindOfClass:[NSArray class]]) {
        NSArray* array = (NSArray*)data;
        for (NSDictionary* dict in array) {
            PlaceInfo* info = [[PlaceInfo alloc] init];
            info.shopid = [dict objectForKey:@"c_shop_id"];
            info.name = [dict objectForKey:@"name"];
            info.review = [dict objectForKey:@"pinyin"];
            info.imageurl = [dict objectForKey:@"image_url"];
            [traveldetailinfoArray addObject:info];
            [info release];
        }
    }
    return traveldetailinfoArray;
}

- (TripInfo*)tripinfoParser:(NSData*)data
{
    TripInfo* tripinfo = [[[TripInfo alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* infoDict = (NSDictionary*)data;
        tripinfo.travellineid = [infoDict objectForKey:@"c_travel_line_id"];
        tripinfo.name = [infoDict objectForKey:@"name"];
        tripinfo.author = [infoDict objectForKey:@"author"];
        tripinfo.creattime = [infoDict objectForKey:@"create_time"];
        tripinfo.memberid = [infoDict objectForKey:@"c_member_id"];
        tripinfo.coverurl = [infoDict objectForKey:@"cover"];
        tripinfo.praisecount = [infoDict objectForKey:@"praise_count"];
        tripinfo.favorcount = [infoDict objectForKey:@"favor_count"];
        tripinfo.starttime = [infoDict objectForKey:@"start_time"];
        tripinfo.endtime = [infoDict objectForKey:@"end_time"];
        tripinfo.recommondpoint = [infoDict objectForKey:@"recommend_point"];
        tripinfo.commentcount = [infoDict objectForKey:@"comment_count"];
        tripinfo.detail = [infoDict objectForKey:@"detail"];
        tripinfo.fromcityid = [infoDict objectForKey:@"c_from_city_id"];
        
        id eventList = [infoDict objectForKey:@"event_list"];
        int index = 0;
        if (eventList && [eventList isKindOfClass:[NSDictionary class]]) {
            NSArray* keyArray = [eventList allKeys];
            for (NSString* key in keyArray) {
                NSDictionary* dateDic = [eventList objectForKey:key];
                NSMutableArray* addEventArray = [[NSMutableArray alloc] init];
                NSString* cityName = [dateDic objectForKey:@"city"];
                NSString* cityid = @"";
                for (CityInfo* city in [DataSource shareInstance].cityArray){
                    if ([cityName isKindOfClass:[NSNull class]]) {
                        cityName = @"北京";
                        cityid = @"1";
                        break;
                    }
                    if ([cityName isEqualToString:city.name]) {
                        cityid = [NSString stringWithString:city.cityid];
                        break;
                    }
                }
                if ([tripinfo.fromcityid isEqualToString:@"0"]) {
                    tripinfo.fromcityid = cityid;
                }
                NSArray* eventArray = [dateDic objectForKey:@"list"];
                for (NSDictionary* event in eventArray) {
                    PlaceInfo* info = [[PlaceInfo alloc] init];
                    info.name = [event objectForKey:@"name"];
                    info.imageurl = [event objectForKey:@"image"];
                    info.shopid = [event objectForKey:@"c_shop_id"];
                    info.eventid = [event objectForKey:@"c_travel_event_id"];
                    info.typeid = [event objectForKey:@"type"];
                    info.cityid = [NSString stringWithFormat:@"%@",cityid];
                    
                    [addEventArray addObject:info];
                    [info release];
                }
                NSDate* date = [NSDate dateWithDateString:(NSString*)[infoDict objectForKey:@"start_time"]] ;
                NSTimeInterval timeInterval = [date timeIntervalSinceReferenceDate];
                timeInterval += 24*60*60*(index);
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                date = [NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSString* dateString = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:date]];
                [dateFormatter release];
                
                [tripinfo.datedic setObject:addEventArray forKey:dateString];
                //                [addEventArray removeAllObjects];
                //                [addEventArray release];addEventArray = nil;
                
                index ++;
            }
        }
        if (eventList && [eventList isKindOfClass:[NSArray class]]) {
            for (NSDictionary* dateDic in eventList) {
                NSMutableArray* addEventArray = [[NSMutableArray alloc] init];
                NSString* cityName = [dateDic objectForKey:@"city"];
                NSString* cityid = @"";
                if ([cityName isKindOfClass:[NSNull class]]) {
                    cityName = @"北京";
                }
                for (CityInfo* city in [DataSource shareInstance].cityArray) {
                    if ([cityName isEqualToString:city.name]) {
                        cityid = [NSString stringWithString:city.cityid];
                        break;
                    }
                }
                NSArray* eventArray = [dateDic objectForKey:@"list"];
                for (NSDictionary* event in eventArray) {
                    PlaceInfo* info = [[PlaceInfo alloc] init];
                    info.name = [event objectForKey:@"name"];
                    info.imageurl = [event objectForKey:@"image"];
                    info.shopid = [event objectForKey:@"c_shop_id"];
                    info.eventid = [event objectForKey:@"c_travel_event_id"];
                    info.typeid = [event objectForKey:@"type"];
                    info.cityid = [NSString stringWithFormat:@"%@",cityid];
                    
                    [addEventArray addObject:info];
                    [info release];
                }
                NSDate* date = [NSDate dateWithDateString:(NSString*)[infoDict objectForKey:@"start_time"]] ;
                NSTimeInterval timeInterval = [date timeIntervalSinceReferenceDate];
                timeInterval += 24*60*60*(index);
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                date = [NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval];
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                NSString* dateString = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:date]];
                [dateFormatter release];
                
                [tripinfo.datedic setObject:addEventArray forKey:dateString];
//                [addEventArray removeAllObjects];
//                [addEventArray release];addEventArray = nil;
                index ++;
            }
        }
    }
    return tripinfo;
}

- (MyTripInfo*)mytripinfoParser:(NSData*)data
{
    MyTripInfo* mytripInfo = [[[MyTripInfo alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* mytripinfoDict = (NSDictionary*)data;
        
        id datainfoArray = [mytripinfoDict objectForKey:@"data"];
        NSMutableArray* infoArray = [[NSMutableArray alloc] init];
        if (datainfoArray &&[datainfoArray isKindOfClass:[NSArray class]]) {
            NSArray* array = (NSArray*)datainfoArray;
            for (NSDictionary* infoDict in array) {
                TripInfo* tripinfo = [[TripInfo alloc] init];
                tripinfo.travellineid = [infoDict objectForKey:@"c_travel_line_id"];
                tripinfo.name = [infoDict objectForKey:@"name"];
                tripinfo.creattime = [infoDict objectForKey:@"create_time"];
                tripinfo.memberid = [infoDict objectForKey:@"c_member_id"];
                tripinfo.coverurl = [infoDict objectForKey:@"cover"];
                tripinfo.praisecount = [infoDict objectForKey:@"praise_count"];
                tripinfo.favorcount = [infoDict objectForKey:@"favor_count"];
                tripinfo.starttime = [infoDict objectForKey:@"start_time"];
                tripinfo.endtime = [infoDict objectForKey:@"end_time"];
                tripinfo.recommondpoint = [infoDict objectForKey:@"recommend_point"];
                tripinfo.commentcount = [infoDict objectForKey:@"comment_count"];
                tripinfo.membericon = [infoDict objectForKey:@"c_member_icon"];
                [infoArray addObject:tripinfo];
                [tripinfo release];
            }
        }
        [mytripInfo.tripinfoArray addObjectsFromArray:infoArray];
        [infoArray removeAllObjects];
        [infoArray release];
        
        id mytripPageDict = [mytripinfoDict objectForKey:@"navi"];
        if ([mytripPageDict isKindOfClass:[NSDictionary class]]) {
            NSNumber* currentPageNumber = [mytripPageDict objectForKey:@"current_page"];
            mytripInfo.currentPageId = [currentPageNumber intValue];
            mytripInfo.nextPage = (BOOL)[mytripPageDict objectForKey:@"next"];
            NSNumber* totlePageCountNumber = [mytripPageDict objectForKey:@"total_page"];
            if (![totlePageCountNumber isKindOfClass:[NSNull class]]) {
                mytripInfo.totlePageCount = [totlePageCountNumber intValue];
            }
        }
    }
    return mytripInfo;
}

- (PlaceDetailInfo*)placeDetailinfoParser:(NSData*)data
{
    PlaceDetailInfo* placeDetailinfo = [[[PlaceDetailInfo alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* infoDict = (NSDictionary*)data;
        placeDetailinfo.shopid = [infoDict objectForKey:@"c_shop_id"];
        placeDetailinfo.name = [infoDict objectForKey:@"name"];
        placeDetailinfo.review = [infoDict objectForKey:@"review"];
        placeDetailinfo.imageurl = [infoDict objectForKey:@"image_url"];
        placeDetailinfo.typeid = [infoDict objectForKey:@"element_type"];
        placeDetailinfo.telephone = [infoDict objectForKey:@"telephone"];
        if ([[infoDict objectForKey:@"langitude"] isKindOfClass:[NSNumber class]]) {
            NSNumber* number = [infoDict objectForKey:@"langitude"];
            placeDetailinfo.langitude = [number stringValue];
        }
        else
        {
            placeDetailinfo.langitude = [infoDict objectForKey:@"langitude"];
        }
        if ([[infoDict objectForKey:@"latitude"] isKindOfClass:[NSNumber class]]) {
            NSNumber* number = [infoDict objectForKey:@"latitude"];
            placeDetailinfo.latitude = [number stringValue];
        }
        else
        {
            placeDetailinfo.latitude = [infoDict objectForKey:@"latitude"];
        }
        placeDetailinfo.adress = [infoDict objectForKey:@"adress"];
        placeDetailinfo.star = @"";//[infoDict objectForKey:@"star"];
    }

    return placeDetailinfo;
}

- (NSDictionary*)intertEventListParser:(NSData*)data
{
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* dict = [[[NSDictionary alloc] initWithDictionary:(NSDictionary*)data] autorelease];
        return dict;
    }
    
    return nil;
}

- (CommentList*)commentinfoListParser:(NSData*)data
{
    CommentList* commentList = [[[CommentList alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* commentDict = (NSDictionary*)data;
        
        NSArray* datainfoArray = [commentDict objectForKey:@"data"];
        [commentList.commentArray addObjectsFromArray:datainfoArray];
        
        id mytripPageDict = [commentDict objectForKey:@"navi"];
        if ([mytripPageDict isKindOfClass:[NSDictionary class]]) {
            NSNumber* currentPageNumber = [mytripPageDict objectForKey:@"current_page"];
            commentList.currentPageId = [currentPageNumber intValue];
            commentList.nextPage = (BOOL)[mytripPageDict objectForKey:@"next"];
            NSNumber* totlePageCountNumber = [mytripPageDict objectForKey:@"total_page"];
            if (![totlePageCountNumber isKindOfClass:[NSNull class]]) {
                commentList.totlePageCount = [totlePageCountNumber intValue];
            }
        }
    }
    return commentList;
}

- (MessageInfoList*)mycommentinfoParser:(NSData*)data
{
//    NSMutableArray* mycommentinfoArray = [[[NSMutableArray alloc] init] autorelease];
    MessageInfoList* messageInfoList = [[[MessageInfoList alloc] init] autorelease];
    if ([data isKindOfClass:[NSDictionary class]]) {
        NSDictionary* mymessageDict = (NSDictionary*)data;
        
        id messageinfoArray = [mymessageDict objectForKey:@"data"];
        if ([messageinfoArray isKindOfClass:[NSArray class]]) {
            for (NSDictionary* dict in messageinfoArray) {
                MessageInfo* info = [[MessageInfo alloc] init];
                info.travellineid = [dict objectForKey:@"c_travel_line_id"];
                info.name = [dict objectForKey:@"name"];
                info.coverurl = [dict objectForKey:@"cover"];
                info.commentcount = [dict objectForKey:@"comment_count"];
                info.newcommentcount = [dict objectForKey:@"new_comment_count"];
                info.creattime = [dict objectForKey:@"create_time"];
                [messageInfoList.messageArray addObject:info];
                [info release];
            }
        }
        id mymessagePageDict = [mymessageDict objectForKey:@"navi"];
        if ([mymessagePageDict isKindOfClass:[NSDictionary class]]) {
            NSNumber* currentPageNumber = [mymessagePageDict objectForKey:@"current_page"];
            messageInfoList.currentPageId = [currentPageNumber intValue];
            messageInfoList.nextPage = (BOOL)[mymessagePageDict objectForKey:@"next"];
            NSNumber* totlePageCountNumber = [mymessagePageDict objectForKey:@"total_page"];
            if ([totlePageCountNumber isKindOfClass:[NSNumber class]]) {
                messageInfoList.totlePageCount = [totlePageCountNumber intValue];
            }
        }
    }
    return messageInfoList;
}


- (NSMutableArray*)recommendmapeventParser:(NSData*)data
{
    NSMutableArray* recommendmapeventArray = [[[NSMutableArray alloc] init] autorelease];
    if ([data isKindOfClass:[NSArray class]]) {
        NSArray* eventArray = (NSArray*)data;
        for (NSDictionary* event in eventArray) {
            if ([event isKindOfClass:[NSDictionary class]]) {
                PlaceDetailInfo* placeDetailinfo = [[PlaceDetailInfo alloc] init];
                NSDictionary* infoDict = (NSDictionary*)event;
                placeDetailinfo.shopid = [infoDict objectForKey:@"c_shop_id"];
                placeDetailinfo.name = [infoDict objectForKey:@"name"];
                placeDetailinfo.review = [infoDict objectForKey:@"review"];
                placeDetailinfo.imageurl = [infoDict objectForKey:@"image_url"];
                placeDetailinfo.typeid = [infoDict objectForKey:@"element_type"];
                placeDetailinfo.telephone = [infoDict objectForKey:@"telephone"];
                placeDetailinfo.langitude = [infoDict objectForKey:@"langitude"];
                placeDetailinfo.latitude = [infoDict objectForKey:@"latitude"];
                placeDetailinfo.adress = [infoDict objectForKey:@"adress"];
                placeDetailinfo.star = @"";//[infoDict objectForKey:@"star"];
                
                [recommendmapeventArray addObject:placeDetailinfo];
                [placeDetailinfo release];
            }
        }
    }
    return recommendmapeventArray;
}

@end
